package com.ohuang.imghide.util;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class BitmapUtil {

    public static final String TAG = BitmapUtil.class.getSimpleName();

    /**
     * 缩放到对应尺寸
     *
     * @param bitmap
     * @param toWidth
     * @param toHeight
     * @return
     */
    public static Bitmap Scale(Bitmap bitmap, int toWidth, int toHeight) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        Matrix matrix = new Matrix();

        matrix.postScale(toWidth * 1.0f / width, toHeight * 1.0f / height);

        Bitmap dstbmp = Bitmap.createBitmap(
                bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                matrix, true
        );

        return dstbmp;
    }

    /**
     * 灰度化
     *
     * @param bitmap
     * @return
     */
    public static Bitmap Grayscale(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap bitmap1 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        int[] mapPix = new int[width * height];
        bitmap.getPixels(mapPix, 0, width, 0, 0, width, height);
        int[] bitmapPix = new int[width * height];
        for (int i = 0; i < bitmapPix.length; i++) {
            bitmapPix[i] = getGrayscaleColor(mapPix[i]);
        }
        bitmap1.setPixels(bitmapPix, 0, width, 0, 0, width, height);
        return bitmap1;
    }

    /**
     * 颜色灰度化
     *
     * @param color
     * @return
     */
    public static int getGrayscaleColor(int color) {
        int a = Color.alpha(color);
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        int t = (int) (0.3 * r + 0.59 * g + 0.11 * b);
        return Color.argb(a, t, t, t);
    }

    /**
     * 缩放到对应尺寸
     *
     * @param bitmap
     * @param toWidth
     * @param toHeight
     * @return
     */
    @Deprecated
    public static Bitmap changeSize(Bitmap bitmap, int toWidth, int toHeight) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] mapPix = new int[width * height];
        bitmap.getPixels(mapPix, 0, width, 0, 0, width, height);
        Bitmap bitmap1 = Bitmap.createBitmap(toWidth, toHeight, Bitmap.Config.ARGB_8888);
        int[] bitmapPix = new int[toWidth * toHeight];
        for (int i = 0; i < bitmapPix.length; i++) {
            setPix(i % toWidth, i / toWidth, width, height, toWidth, toHeight, mapPix, bitmapPix);
        }
        bitmap1.setPixels(bitmapPix, 0, toWidth, 0, 0, toWidth, toHeight);

        return bitmap1;
    }


    private static void setPix(int x, int y, int w1, int h1, int w2, int h2, int[] b1, int[] b2) {
        float dw = (w1 * 1.0f) / w2;
        float x0 = dw * x;
        float x1 = dw * (x + 1);
        float dh = (h1 * 1.0f) / h2;
        float y0 = dh * y;
        float y1 = dh * (y + 1);

        int color = 0;
        for (float i = x0; i < x1; ) {
            int t = (int) i + 1;
            float dpx = 0;
            if (t < x1) {
                dpx = (t - i) / dw;
            } else {
                dpx = (x1 - i) / dw;
            }

            for (float j = y0; j < y1; ) {
                int p = (int) j + 1;
                float dpy = 0;
                if (p < y1) {
                    dpy = (p - j) / dh;
                } else {
                    dpy = (y1 - j) / dh;
                }
                int i1 = (p - 1) * w1 + (t - 1);
                if (i1 < b1.length) {
                    color = getDpColor(color, b1[(p - 1) * w1 + (t - 1)], dpx * dpy);
                }
                j = p;
            }
            i = t;
        }
        b2[y * w2 + x] = color;
    }

    private static int getDpColor(int color, int addColor, float weight) {
        int a = (int) (Color.alpha(color) + Color.alpha(addColor) * weight);
        int r = (int) (Color.red(color) + Color.red(addColor) * weight);
        int g = (int) (Color.green(color) + Color.green(addColor) * weight);
        int b = (int) (Color.blue(color) + Color.blue(addColor) * weight);
        return Color.argb(a, r, g, b);
    }

    public static void saveBitmapFile(Bitmap bitmap, String path) {
        File file = new File(path);//将要保存图⽚的路径
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
